import { PlayerEntity, PlayerLevel } from '../entity/PlayerEntity';
import { PlayerAchievementValue, AchievementType } from '../value/PlayerAchievementValue';

// 玩家领域服务
export class PlayerService {
  // 计算玩家破案奖励经验值
  public calculateCaseRewardExp(
    player: PlayerEntity,
    caseDifficulty: number, // 1-5
    timeTaken: number, // 分钟
    isFirstAttempt: boolean
  ): number {
    let baseExp = caseDifficulty * 20;
    
    // 难度加成
    baseExp *= caseDifficulty;
    
    // 时间效率加成
    if (timeTaken < 30) {
      baseExp *= 1.5; // 30分钟内完成，1.5倍
    } else if (timeTaken < 60) {
      baseExp *= 1.2; // 1小时内完成，1.2倍
    }
    
    // 首次尝试加成
    if (isFirstAttempt) {
      baseExp *= 1.3;
    }
    
    // 等级平衡
    switch (player.level) {
      case PlayerLevel.NOVICE:
        baseExp *= 1.2;
        break;
      case PlayerLevel.ASSISTANT:
        baseExp *= 1.1;
        break;
      case PlayerLevel.SENIOR:
      case PlayerLevel.MASTER:
        baseExp *= 0.9;
        break;
    }
    
    return Math.round(baseExp);
  }
  
  // 更新玩家准确率
  public updateAccuracy(player: PlayerEntity, correct: boolean): void {
    const totalCases = player.solvedCases + 1;
    const newAccuracy = ((player.accuracy * player.solvedCases) + (correct ? 100 : 0)) / totalCases;
    player.accuracy = Math.round(newAccuracy);
  }
  
  // 检查并生成可能的成就
  public checkForNewAchievements(
    player: PlayerEntity,
    currentAchievements: PlayerAchievementValue[]
  ): PlayerAchievementValue[] {
    const newAchievements: PlayerAchievementValue[] = [];
    const existingIds = currentAchievements.map(a => a.id);
    
    // 检查破案数量成就
    if (player.solvedCases >= 1 && !existingIds.includes('first_case')) {
      newAchievements.push(this.createAchievement(
        'first_case',
        player.id,
        '初露锋芒',
        '完成第一个案件的调查',
        AchievementType.CASE_SOLVED,
        '/icons/achievements/first_case.png',
        50
      ));
    }
    
    if (player.solvedCases >= 10 && !existingIds.includes('detective_10')) {
      newAchievements.push(this.createAchievement(
        'detective_10',
        player.id,
        '小有成就',
        '成功侦破10个案件',
        AchievementType.CASE_SOLVED,
        '/icons/achievements/detective_10.png',
        200
      ));
    }
    
    if (player.solvedCases >= 50 && !existingIds.includes('master_50')) {
      newAchievements.push(this.createAchievement(
        'master_50',
        player.id,
        '侦探大师',
        '成功侦破50个案件',
        AchievementType.CASE_SOLVED,
        '/icons/achievements/master_50.png',
        1000
      ));
    }
    
    // 检查准确率成就
    if (player.accuracy >= 90 && player.solvedCases >= 5 && !existingIds.includes('high_accuracy')) {
      newAchievements.push(this.createAchievement(
        'high_accuracy',
        player.id,
        '明察秋毫',
        '破案准确率达到90%以上',
        AchievementType.ACCURACY,
        '/icons/achievements/high_accuracy.png',
        300
      ));
    }
    
    return newAchievements;
  }
  
  // 创建成就实例
  private createAchievement(
    id: string,
    playerId: string,
    name: string,
    description: string,
    type: AchievementType,
    iconUrl: string,
    points: number
  ): PlayerAchievementValue {
    return new PlayerAchievementValue(
      id,
      playerId,
      name,
      description,
      type,
      iconUrl,
      new Date().toISOString(),
      points
    );
  }
  
  // 获取玩家等级进度百分比
  public getLevelProgressPercentage(player: PlayerEntity): number {
    const currentExp = player.experience;
    const nextLevelExp = player.getNextLevelExp();
    
    if (player.level === PlayerLevel.MASTER) {
      return 100; // 最高等级
    }
    
    return Math.min(100, Math.round((currentExp / nextLevelExp) * 100));
  }
}
